package com.atguigu.gmall.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.IpUtil;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/user/passport")
public class PassportApiController {

    @Autowired
    UserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("login")
    public Result login(@RequestBody UserInfo userInfo,
                        HttpServletRequest request){

        UserInfo info = userService.login(userInfo);

        if(info!=null){

            String token = UUID.randomUUID().toString();

            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("token",token);
            hashMap.put("nickName",info.getNickName());

            String userLoginKey = RedisConst.USER_LOGIN_KEY_PREFIX+token;
            JSONObject useJson = new JSONObject();
            useJson.put("userId",info.getId().toString());
            useJson.put("ip",IpUtil.getIpAddress(request));
            redisTemplate.opsForValue().set(userLoginKey,useJson.toJSONString(),RedisConst.USERKEY_TIMEOUT,TimeUnit.SECONDS);
            return Result.ok(hashMap);
        }else {
            return Result.fail().message("您输入的用户名或密码错误");
        }
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("logout")
    public Result logout(HttpServletRequest request){

        String token = request.getHeader("token");

        redisTemplate.delete(RedisConst.USER_LOGIN_KEY_PREFIX+token);

        return Result.ok();
    }
}
